package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.enums.ApiErrorCode;
import com.example.demo.entity.UserEntity;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @DESCRIPTION:
 * @AUTOOR: Lee
 * @DATE: 2022年09月18日 0:47
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/create")
    public R<UserEntity> createUser(@RequestBody UserEntity user) {
        UserEntity userEntity = userService.createUser(user);
        return R.restResult(userEntity, ApiErrorCode.SUCCESS.getCode(), "创建用户成功");
    }

    @PostMapping(value = "/login")
    public R<String> login(HttpServletRequest request,
                           HttpServletResponse response,
                           @RequestParam("userName") String userName,
                           @RequestParam("passWord") String passWord) {
        userService.login(request, response, userName, passWord);
        return R.ok("登录成功");
    }
}
